home *** CD-ROM | disk | FTP | other *** search
- Path: beta.nedernet.nl!usenet
- From: jos@and.nl (Jos A. Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: fast find algorithm
- Date: 3 Apr 1996 10:33:00 GMT
- Organization: AND Operations Research B.V.
- Message-ID: <4jtk4t$g6r@beta.nedernet.nl>
- References: <Dp8wE6.8DG@cix.compulink.co.uk>
- NNTP-Posting-Host: klepzeiker.and.nl
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <Dp8wE6.8DG@cix.compulink.co.uk>, setheridge@cix.compulink.co.uk
- wrote:
-
- |Does anyone have an search algoritm faster than a binary chop for the
- |following:
- |
- |find a date from a sorted array of 1500 possible storage locations
-
- It depends on the distribution of those 1500 keys, but if those keys
- are (more or less) uniformly distributed _and_ those keys have type
- double, you could give this a try:
-
- let n be the size of the array 'key' and let k be the key we're looking for.
- Apply a modified binary chop as follows:
-
- int l, m, h;
-
- for (l= 0, h= n-1; l <= h; ) {
-
- m = (h-l)*(k-key[l])/(key[h]-key[l])+l;
-
- if (array[m] == k)
- /* found */
- else if (array[m] < k)
- l= m+1;
- else
- h= m-1;
- }
-
- On average, this algorithm is (theoretically) faster than a simple
- binary chop, where location m is defined as: 'm= (h+l)/2'. But, again,
- it all depends on the distribution of the key values ...
-
- kind regards,
-
- Jos aka jos@and.nl
- --
- Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
-
-